<?php

	/*
	 * All rights Reserved, Designed By www.youyacao.com <br>
	 * @Description:用户 <br>
	 * @version 蜻蜓C影视追剧系统  <br>
	 * @author:成都市一颗优雅草科技有限公司  <br>
	 * 注意：使用我司开源源代码请遵循license文件的协议仅供个人非盈利使用，禁止用于其他的商业用途
	 * 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
	 * 正版系统查询系统 zhengbancha.youyacao.com
	 */
namespace App\Admin\Controllers;

use App\Models\CardType;
use App\Models\Department;
use App\Models\Mp;
use App\Models\User;
use App\Models\UserCard;
use App\Models\UserRole;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use App\Admin\Metrics\Examples\NewDevices;
use App\Admin\Metrics\Examples\NewUsers;
use App\Admin\Metrics\User\TotalUsers;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('用户信息')
            ->description('描述')
//            ->body(function (Row $row) {
//                $row->column(4, new TotalUsers());
//            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('username')->display(function ($username) {
                if(empty($username)){
                    return "<p style='color:red;'>未设置</p>";
                }else{
                    return "用户名：".$username."<br>邮箱:".$this->email;
                }
            });
            $grid->column('phone');
//            $grid->column('email');
//            $grid->column('password');
            $grid->column('nickname');
//            $grid->column('avatar')->image('',100,100);
//            $grid->column('refcode');
//            $grid->column('truename');
//            $grid->column('qq');
//            $grid->column('sex')->using([0=>'保密',1 => '男', 2 => '女'])->label([
//                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
//                1 => 'primary',
//                2 => 'danger',
//            ]);//性别0保密1男2女
//            $grid->column('birthday');
//            $grid->column('department_id')->display(function ($id){
//                if(is_null($id) ){
//                    return "<p style='color:red;'>未设置</p>";
//                }else{
//                    return $this->department->name;
//                }
//            });
//            $grid->column('integral');
//            $grid->column('gold');
//            $grid->column('heat');
//            $grid->column('grade');
//            $grid->column('alipay_account_name');
//            $grid->column('alipay_account');
//            $grid->column('vip_end_time');
//            $grid->column('position');
//            $grid->column('desc');
//            $grid->column('pid');
//            $grid->column('level');
//            $grid->column('is_auth');
//            $grid->column('is_audio');
//            $grid->column('api_token');
//            $grid->column('wx_openid');
//            $grid->column('qq_openid');
//            $grid->column('wb_openid');
//            $grid->column('device_id');
//            $grid->column('is_admin');
//            $grid->column('is_super');
            $grid->column('last_login_time');

//            $grid->column('tags');
//            $grid->column('can_live');
//            $grid->column('share_free_num');
            $grid->column('role_id', '角色')->using(UserRole::pluck('name', 'id')->toArray())->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                1 => 'primary',
                2 => 'success',
            ]);
            $grid->column('card', '卡等级')->display(function () {
                $cardmodel=UserCard::query()->where('user_id', $this->id)->first();
                if (is_null($cardmodel)) {
                    return "<p style='color:red;'>未绑卡</p>";
                } else {
                    return CardType::query()->where('id', $cardmodel->card_id)->first()->name;
                }
            });
            $grid->column('status')->switch();//状态0:删除1:正常2:禁用
//            $grid->quickSearch(['id', 'username', 'phone', 'nickname', 'truename', 'mps.name'])->placeholder('可搜索ID,手机号，账户，昵称,真实姓名');
            $grid->column('created_at');
            $grid->async(false);
//            $grid->column('updated_at')->sortable();
//            $grid->disableCreateButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('phone');
            $show->field('email');
            $show->field('password');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('refcode');
            $show->field('truename');
            $show->field('qq');
            $show->field('sex');
            $show->field('birthday');
            $show->field('amount');
            $show->field('integral');
            $show->field('gold');
            $show->field('heat');
            $show->field('grade');
            $show->field('alipay_account_name');
            $show->field('alipay_account');
            $show->field('vip_end_time');
            $show->field('position');
            $show->field('desc');
            $show->field('pid');
            $show->field('level');
            $show->field('is_auth');
            $show->field('is_audio');
            $show->field('api_token');
            $show->field('wx_openid');
            $show->field('qq_openid');
            $show->field('wb_openid');
            $show->field('device_id');
            $show->field('is_admin');
            $show->field('is_super');
            $show->field('last_login_time');
            $show->field('tags');
            $show->field('can_live');
            $show->field('share_free_num');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('username')->required();
            $form->mobile('phone');
            $form->email('email');
            $form->text('password')->required();
            $form->text('nickname');
            $form->select('role_id', '角色')->options(UserRole::pluck('name', 'id')->toArray())->required();
            $form->image('avatar')->autoUpload();
//            $form->text('refcode');
//            $form->select('department_id')->options(Department::all()->pluck('name', 'id'));
//            $form->text('truename');
//            $form->text('qq');
//            $form->text('sex');
//            $form->text('birthday');
//            $form->text('amount');
//            $form->text('integral');
//            $form->text('gold');
//            $form->text('heat');
//            $form->text('grade');
//            $form->text('alipay_account_name');
//            $form->text('alipay_account');
//            $form->text('vip_end_time');
//            $form->text('position');
//            $form->text('desc');
//            $form->text('pid');
//            $form->text('level');
//            $form->text('is_auth');
//            $form->text('is_audio');
//            $form->text('api_token');
//            $form->text('wx_openid');
//            $form->text('qq_openid');
//            $form->text('wb_openid');
//            $form->text('device_id');
//            $form->text('is_admin');
//            $form->text('is_super');
//            $form->text('last_login_time');
//            $form->text('tags');
//            $form->text('can_live');
//            $form->text('share_free_num');

            $form->radio('status')->options([1 => '正常', 0 => '禁用'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        })->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });
    }
}
